package com.aididi.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Auther: zx.xu
 * @Description:
 */
@Component
@Slf4j
public class SessionHandler {

    private final Map<String, WebSocketSession> sessionMap = new ConcurrentHashMap<>();

    public SessionHandler(){}

    public void register(WebSocketSession session){
        sessionMap.put(session.getPrincipal().getName(), session);
    }

    public void remove(WebSocketSession session){
        sessionMap.remove(session.getPrincipal().getName());
    }

    /**
     * 查看是否在线
     * @param openid
     * @return
     */
    public boolean isOnline(String openid){
        return sessionMap.get(openid) != null;
    }
}
